{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 (1200, 1600, 3)\n",
      "2 (1200, 1600, 3)\n",
      "3 (1200, 1600, 3)\n",
      "4 (1200, 1600, 3)\n",
      "5 (1200, 1600, 3)\n",
      "6 (1200, 1600, 3)\n",
      "7 (1200, 1600, 3)\n",
      "8 (1200, 1600, 3)\n",
      "9 (1200, 1600, 3)\n",
      "end!\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "#读取一张图片size（高，宽）经常输出空，将mp4改为wmv再输出一次，来回改输出就可以了,notebook跑完后台没跑完，要等挺久的，不然文件0k\n",
    "size = (1200,1600)\n",
    "\n",
    "videowrite = cv2.VideoWriter(r'D:\\test12.mp4',-1,3,size)#20是帧数，size是图片尺寸\n",
    "img_array=[]\n",
    "for filename in [r'D:\\image\\{0:04d}.jpg'.format(i) for i in range(1,11)]:\n",
    "    img = cv2.imread(filename)\n",
    "    if img is None:\n",
    "        print(filename + \" is error!\")\n",
    "        continue\n",
    "    img_array.append(img)\n",
    "\n",
    "for i in range(1,10):\n",
    "    print(i,img_array[i].shape)\n",
    "    videowrite.write(img_array[i])\n",
    "print('end!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "end!\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "from  matplotlib import pyplot as plt\n",
    "\n",
    "video_name = 'output_video.mp4'\n",
    "fourcc = cv2.VideoWriter_fourcc(*'XVID')\n",
    "#fourcc = cv2.VideoWriter_fourcc(*'MP4V')\n",
    "fps = 10  # 每秒帧数\n",
    "size = (1600, 1200)  # 图片大小\n",
    "\n",
    "# 创建视频写入对象\n",
    "out = cv2.VideoWriter(video_name, fourcc, fps, size)\n",
    "\n",
    "for filename in [r'D:\\image\\{0:04d}.jpg'.format(i) for i in range(1,2520)]:\n",
    "    img = cv2.imread(filename)\n",
    "\n",
    "    if img is None:\n",
    "        print(filename + \" is error!\")\n",
    "        continue\n",
    "    out.write(img)\n",
    "\n",
    "print('end!')\n",
    "out.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
